﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;

namespace KPIS.GERP.GHRMS.APP.App_UI.UserControls
{
    public partial class RC_TransferQualOfficialPlus : System.Web.UI.UserControl
    {
        private SsMng ssMng = new SsMng();
        private RadTextBox txttempEmployee, txtOldPosition, txtOldJobsiteName;
        private RadNumericTextBox txtOldSalary;
        private Label lblBirthDate, lblCitizen, lblTmpEmpSeq;
        private RadComboBox cmbTmpEdu, movmentCombo, oldPosTypeCombo, oldClassCombo;

        private object _dataItem = null;
        public object DataItem
        {
            get
            {
                return this._dataItem;
            }
            set
            {
                this._dataItem = value;
            }
        }
        protected bool IsUserControlPostBack
        {
            get
            {
                return this.ViewState["IsUserControlPostBack"] != null;
            }
        }

        protected override void OnLoad(System.EventArgs e)
        {
            base.OnLoad(e);
            ControlConfig();

            if (!IsUserControlPostBack)
            {                
                this.ViewState.Add("IsUserControlPostBack", true);
            }

            if (DataItem != null)
            {
                bindPosTypeCombo(posTypeCombo);
                bindPosTypeCombo(entPostTypeCombo);
                bindJobSiteCombo();                

                if (!(DataItem is Telerik.Web.UI.GridInsertionObject))
                {

                    lblSequence.Text = DataBinder.Eval(DataItem, "cmd_add_seq").ToString();
                    lblTmpEmpSeq.Text = DataBinder.Eval(DataItem, "tmp_emp_seq").ToString();
                    lblCmdSeq.Text = DataBinder.Eval(DataItem, "cmd_seq").ToString();

                    txttempEmployee.Text = DataBinder.Eval(DataItem, "full_name").ToString();
                    lblBirthDate.Text = DataBinder.Eval(DataItem, "birth_date", "{0:dd/MM/yyyy}").ToString();
                    lblCitizen.Text = DataBinder.Eval(DataItem, "citizen").ToString();

                    CMD_EmployeeInfo1.bindMovmentCombo();
                    CMD_EmployeeInfo1.bindTmpEducateCombo();
                    cmbTmpEdu.SelectedValue = DataBinder.Eval(DataItem, "tmp_edu_seq").ToString();
                    movmentCombo.SelectedValue = DataBinder.Eval(DataItem, "move_seq").ToString();

                    // ตำแหน่งและส่วนราชการเดิม
                    txtOldPosition.Text = DataBinder.Eval(DataItem, "old_pos_name").ToString();
                    txtOldJobsiteName.Text = DataBinder.Eval(DataItem, "old_job_site_name").ToString();
                    txtOldSalary.Text = DataBinder.Eval(DataItem, "old_salary").ToString();
                    CMD_FieldPositionInfo1.bindPosTypeCombo();
                    oldPosTypeCombo.SelectedValue = DataBinder.Eval(DataItem, "old_pos_type_seq").ToString();
                    CMD_FieldPositionInfo1.bindClasscombo();
                    oldClassCombo.SelectedValue = DataBinder.Eval(DataItem, "old_class_seq").ToString();

                    //ตำแหน่งที่สอบแข่งขันได้
                     txtEntPosition.Text = DataBinder.Eval(DataItem, "ent_pos_name").ToString();                     
                     entPostTypeCombo.SelectedValue = DataBinder.Eval(DataItem, "ent_pos_type_seq").ToString();
                     bindClasscombo(entClassCombo, entPostTypeCombo.SelectedValue);
                     entClassCombo.SelectedValue = DataBinder.Eval(DataItem, "ent_class_seq").ToString();
                     txtEntNo.Text = DataBinder.Eval(DataItem, "ent_no").ToString();
                     txtEntJobSiteName.Text = DataBinder.Eval(DataItem, "ent_job_site_name ").ToString();
                     txtEntDate.SetValue(DataBinder.Eval(DataItem, "ent_date", "{0:dd/MM/yyyy}").ToString());  

                    //ตำแหน่งและส่วนราชการที่รับโอน
                    txtPosition.Text = DataBinder.Eval(DataItem, "pos_name").ToString();
                    txtPosRef.Text = DataBinder.Eval(DataItem, "pos_ref_no").ToString();
             
                    posTypeCombo.SelectedValue = DataBinder.Eval(DataItem, "pos_type_seq").ToString();
                    bindClasscombo(classCombo, posTypeCombo.SelectedValue);
                    classCombo.SelectedValue = DataBinder.Eval(DataItem, "class_seq").ToString();
                    jobSiteCombo.SelectedValue = DataBinder.Eval(DataItem, "job_site_seq").ToString();
                    txtSalary.Text = DataBinder.Eval(DataItem, "salary").ToString();
                    txtStartDate.SetValue(DataBinder.Eval(DataItem, "start_date", "{0:dd/MM/yyyy}").ToString());
                    txtRemarks.Text = DataBinder.Eval(DataItem, "remarks").ToString();
                }
            }
        }

        private void ControlConfig()
        {
            txttempEmployee = (RadTextBox)CMD_EmployeeInfo1.FindControl("txttempEmployee");
            lblBirthDate = (Label)CMD_EmployeeInfo1.FindControl("lblBirthDate");
            lblCitizen = (Label)CMD_EmployeeInfo1.FindControl("lblCitizen");
            lblTmpEmpSeq = (Label)CMD_EmployeeInfo1.FindControl("lblTmpEmpSeq");
            cmbTmpEdu = (RadComboBox)CMD_EmployeeInfo1.FindControl("cmbTmpEdu");
            movmentCombo = (RadComboBox)CMD_EmployeeInfo1.FindControl("movmentCombo");

            txtOldPosition = (RadTextBox)CMD_FieldPositionInfo1.FindControl("txtPosition");
            txtOldJobsiteName = (RadTextBox)CMD_FieldPositionInfo1.FindControl("txtJobSiteName");
            txtOldSalary = (RadNumericTextBox)CMD_FieldPositionInfo1.FindControl("txtSalary");
            oldPosTypeCombo = (RadComboBox)CMD_FieldPositionInfo1.FindControl("posTypeCombo");
            oldClassCombo = (RadComboBox)CMD_FieldPositionInfo1.FindControl("classCombo");

        }
        private void bindClasscombo(RadComboBox classComboBox, string PositionType)
        {            
            IClassBLL cBLL = new ClassBLL();
            IList<ClassInfo> infoList = cBLL.ListClasses(PositionType);

            classComboBox.DataSource = infoList;
            classComboBox.DataBind();
        }
        private void bindJobSiteCombo()
        {
            IJobSiteBLL jsBLL = new JobSiteBLL();
            IList<JobSiteInfo> infoList = jsBLL.ListJobSiteByTypes(SystemInfo._JobsiteExternal);

            jobSiteCombo.DataSource = infoList;
            jobSiteCombo.DataBind();
        }               
        
        private void bindPosTypeCombo(RadComboBox PosTypeComboBox)
        {
            IPositionTypeBLL ptBLL = new PositionTypeBLL();
            IList<PositionTypeInfo> infoList = ptBLL.ListPositionTypes(1);

            PosTypeComboBox.DataSource = infoList;
            PosTypeComboBox.DataBind();
        }

        protected void btnAddnew_Click(object sender, EventArgs e)
        {
            RecruitmentBLL obj = new RecruitmentBLL();

            obj.InsertTransfer(Convert.ToInt32(Session["CMD"].ToString())
                , Convert.ToInt32(lblTmpEmpSeq.Text.Trim())
                , Convert.ToInt32(movmentCombo.SelectedValue)
                , Convert.ToInt32(cmbTmpEdu.SelectedValue)
                , txtOldPosition.Text.Trim()
                , Convert.ToInt32(oldPosTypeCombo.SelectedValue)
                , Convert.ToInt32(oldClassCombo.SelectedValue)
                , txtOldJobsiteName.Text.Trim()
                , Convert.ToDouble(txtOldSalary.Text)             
                , txtPosition.Text.Trim()
                , txtPosRef.Text.Trim()
                , Convert.ToInt32(posTypeCombo.SelectedValue)
                , Convert.ToInt32(classCombo.SelectedValue)
                , Convert.ToInt32(jobSiteCombo.SelectedValue)
                , jobSiteCombo.SelectedItem.Text
                , Convert.ToDouble(txtSalary.Text)  
                , txtEntNo.Text.Trim()
                , txtEntPosition.Text.Trim()
                , Convert.ToInt32(entPostTypeCombo.SelectedValue)
                , Convert.ToInt32(entClassCombo.SelectedValue)
                , txtEntJobSiteName.Text.Trim()
                , DateTime.Parse(txtEntDate.GetStringValue())
                , DateTime.Parse(txtStartDate.GetStringValue())
                , txtRemarks.Text.Trim()
                , ssMng.EmpSeqI
                );

//string  start_date;//ตั้งแต่วันที่
//int cmd_seq,tmp_emp_seq,emp_seq,pos_seq;//รหัสคำสั่ง,รหัส tmp บุคลากร,รหัสตำแหน่ง
//string pos_ref_no,pos_name;//เลขที่ตำแหน่ง,ชื่อตำแหน่ง
//int class_seq,job_site_seq;//รหัสระดับ,รหัสหน่วยงาน
//string job_site_name;//ชื่อหน่วยงาน

//int tmp_edu_seq;// seq วุฒิการศึกษาที่บรรจุ
//string ent_pos_name;//สอบแข่งขันได้ลำดับที่,ตำแหน่งที่สอบแข่งขันได้
//int ent_pos_type_seq, ent_class_seq;//ประภทตำแหน่งที่สอบแข่งขันได้,ระดับตำแหน่งที่สอบแข่งขันได้
//string ent_job_site_name,ent_date;//ตามประกาศผลสอบของ,ลงวันที่
//int ent_no;
//int old_pos_type_seq,old_class_seq; //ประเภทตำแหน่งเดิม,ระดับตำแหน่งเดิม
//string old_job_site_name, old_pos_name;//สังกัดเดิม,ตำแหน่งเดิม
//double old_salary,salary; //เงินเดือนเดิม ,เงินเดือน
            
//int move_seq,pos_type_seq;//ประเภทการเคลื่อนไหว,ตำแหน่งประเภท
//string remarks;//หมายเหตุ
//int created_by;


//cmd_seq = 1;
//move_seq = int.Parse(movmentCombo.SelectedValue);  

//// วุฒิการศึกษา
//tmp_edu_seq = int.Parse(ComboTmpEdu.SelectedValue);   


//// ตำแหน่งและส่วนราชการเดิม

//old_pos_name = txtOldPosition.Text;
//old_pos_type_seq = int.Parse(oldPosTypeCombo.SelectedValue);
//old_class_seq = int.Parse(oldClassCombo.SelectedValue);
//old_job_site_name = txtOldJobsiteName.Text;
//old_salary = Convert.ToDouble(txtOldSalary.Text);
//salary = Convert.ToDouble(txtSalary.Text);    
            

//            // ตำแหน่งที่สอบแข่งขันได้


//ent_pos_name = txtEntPosition.Text;
//ent_pos_type_seq = int.Parse(entPostTypeCombo.SelectedValue);
//ent_class_seq = int.Parse(entClassCombo.SelectedValue);
//ent_no = int.Parse(txtEntNo.Text);
//ent_date = txtEntDate.GetStringValue();
//ent_job_site_name = txtEntJobSiteName.Text;    

////ตำแหน่งและส่วนราชการที่รับโอน 

//pos_name = txtPosition.Text;
//pos_ref_no = txtPosRef.Text;
//class_seq = int.Parse(classCombo.SelectedValue);
//job_site_seq = int.Parse(jobSiteCombo.SelectedValue);
//job_site_name = jobSiteCombo.Text;
//start_date = txtStartDate.GetStringValue();
//remarks = txtRemarks.Text;
//pos_type_seq = int.Parse(posTypeCombo.SelectedValue);



//RC_TransferExamBLL obj = new RC_TransferExamBLL();

//obj.insert(cmd_seq, int.Parse(lblTmpEmpSeq.Text), move_seq, tmp_edu_seq, 
//    old_pos_name, old_pos_type_seq, old_class_seq
//    , old_job_site_name, old_salary, salary, pos_name, pos_ref_no, 
//    class_seq, job_site_seq, job_site_name, start_date,pos_type_seq, 
//    remarks, 1,ent_pos_name,ent_pos_type_seq,ent_class_seq,ent_no,ent_date,ent_job_site_name      );              



        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {

        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            RecruitmentBLL obj = new RecruitmentBLL();

            obj.UpdateTransfer(Convert.ToInt32(lblSequence.Text.Trim())
                , Convert.ToInt32(Session["CMD"].ToString())
                , Convert.ToInt32(lblTmpEmpSeq.Text.Trim())
                , Convert.ToInt32(movmentCombo.SelectedValue)
                , Convert.ToInt32(cmbTmpEdu.SelectedValue)
                , txtOldPosition.Text.Trim()
                , Convert.ToInt32(oldPosTypeCombo.SelectedValue)
                , Convert.ToInt32(oldClassCombo.SelectedValue)
                , txtOldJobsiteName.Text.Trim()
                , Convert.ToDouble(txtOldSalary.Text)
                , txtPosition.Text.Trim()
                , txtPosRef.Text.Trim()
                , Convert.ToInt32(posTypeCombo.SelectedValue)
                , Convert.ToInt32(classCombo.SelectedValue)
                , Convert.ToInt32(jobSiteCombo.SelectedValue)
                , jobSiteCombo.SelectedItem.Text
                , Convert.ToDouble(txtSalary.Text)
                , txtEntNo.Text.Trim()
                , txtEntPosition.Text.Trim()
                , Convert.ToInt32(entPostTypeCombo.SelectedValue)
                , Convert.ToInt32(entClassCombo.SelectedValue)
                , txtEntJobSiteName.Text.Trim()
                , DateTime.Parse(txtEntDate.GetStringValue())
                , DateTime.Parse(txtStartDate.GetStringValue())
                , txtRemarks.Text.Trim()
                , ssMng.EmpSeqI
                );

        }

        protected void posTypeCombo_SelectedIndexChanged(object o, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
        {
            bindClasscombo(classCombo, posTypeCombo.SelectedValue);
        }

        protected void classCombo_SelectedIndexChanged(object o, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
        {

        }   

        protected void btnFindPos_Click(object sender, ImageClickEventArgs e)
        {

            string st = HideOldPos.Value;
            if (st == "")
                return;

            IPositionBLL pBLL = new PositionBLL();
            PositionInfo posInfo = new PositionInfo();
            IList<PositionInfo> infoList = pBLL.ListPositionsBySequence(st);
   
            if(infoList.Count > 0) 
            {
                posInfo = infoList[0];

                txtPosition.Text = posInfo.Name;// dt.Rows[0]["pos_name"].ToString();
                txtPosRef.Text = posInfo.RefNo;// dt.Rows[0]["pos_ref"].ToString();
                posTypeCombo.SelectedValue = posInfo.PositionType.Sequence.ToString();// dt.Rows[0]["pos_type_seq"].ToString();
                bindClasscombo(classCombo, posTypeCombo.SelectedValue);
                jobSiteCombo.SelectedValue = posInfo.JobSite.Sequence.ToString();// dt.Rows[0]["job_site_seq"].ToString(); 
                 
            }

        }

        protected void btnFindPos_Load(object sender, EventArgs e)
        {

            //if (DataItem is Telerik.Web.UI.GridInsertionObject)
            //{
            btnFindPos.Attributes.Add("OnClick", "LinkMDdetail(" + HideOldPos.ClientID + "," + "'Forms/DialogPositions.aspx','../frmIframe.html',700,500)");
            //}


        }

        protected void txtEntPosition_TextChanged(object sender, EventArgs e)
        {


        }

        protected void entClassCombo_SelectedIndexChanged(object o, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
        {
            
        }

        protected void entPostTypeCombo_SelectedIndexChanged(object o, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
        {
            bindClasscombo(entClassCombo, entClassCombo.SelectedValue);
        }


        protected void btnFindEntPos_Click(object sender, ImageClickEventArgs e)
        {
            string st = HideEntPos.Value;
            if (st == "")
                return;

            IFieldBLL fBLL = new FieldBLL();
            IList<FieldInfo> infoList = fBLL.ListFields(st);

            if (infoList.Count > 0)
            {
                txtEntPosition.Text = infoList[0].LineName;
                entPostTypeCombo.SelectedValue = infoList[0].PositionTypes.Sequence.ToString();
                bindClasscombo(entClassCombo, entPostTypeCombo.SelectedValue);
            }
            
        }

        protected void btnFindEntPos_Load(object sender, EventArgs e)
        {
            btnFindEntPos.Attributes.Add("OnClick", "LinkMDdetail(" + HideEntPos.ClientID + "," + "'Forms/DialogFieldPositions.aspx','../frmIframe.html',700,500)");
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        }

    }    
    
